In [ ]:
%matplotlib inline
import pandas as pd
import numpy as np
import math
import scipy

import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go

import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"

Matplotlib¶

A simple plot¶

$y=sin(x)$

In [ ]:
x = np.linspace(0, 2*math.pi, 100)
y = scipy.sin(x)
plt.plot(x,y)
# plt.savefig('line_plot.png', dpi=200)
plt.show()

Title¶

In [ ]:
x = np.linspace(0, 2*math.pi, 10)
y = scipy.sin(x)
fig, ax = plt.subplots()
ax.plot(x,y)
ax.set_title('y = sin(x)')
plt.show()

Axes¶

In [ ]:
x = np.linspace(0, 2*math.pi, 10)
y = scipy.sin(x)
fig, ax = plt.subplots()
ax.plot(x,y)
# set title
ax.set_title('y = sin(x)')
# horizontal axis
ax.set_xlabel('x')
# vertical axis
ax.set_ylabel('y')
plt.show()
/Users/localadmin/opt/anaconda3/envs/TIL6022/lib/python3.7/site-packages/ipykernel_launcher.py:2: DeprecationWarning:

scipy.sin is deprecated and will be removed in SciPy 2.0.0, use numpy.sin instead

Tick¶

Example from a matplotlib website

In [ ]:
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')
# Pad margins so that markers don't get clipped by the axes
plt.margins(0.2)
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()

Line properties¶

For a complete list of line properties, visit this link

An example of line styles are shown bellow:

drawing

Source

In [ ]:
x = np.linspace(0, 2*math.pi, 10)
y = scipy.sin(x)
fig, ax = plt.subplots()
ax.plot(
    x,y, 
    linestyle='dashdot', linewidth=3, 
    marker='o', markerfacecolor='r'
)
# set title
ax.set_title('y = sin(x)')
# horizontal axis
ax.set_xlabel('x')
# vertical axis
ax.set_ylabel('y')
plt.savefig('line_properties_example.png', dpi=200)
plt.show()
In [ ]:
x = np.random.randn(1000)
plt.boxplot(x)
plt.show()

Histogram¶

In [ ]:
x = np.random.randn(1000)
plt.hist(x, bins=100, rwidth=0.8)
plt.savefig('histogram_plot.png', dpi=200)
plt.show()

Hexbin¶

In [ ]:
points = np.random.randn(1000,2)
plt.hexbin(x=points[:,0], y=points[:,1], gridsize=10, cmap='Spectral_r')
plt.savefig('hexbin_plot.png', dpi=200)
plt.show()

Grids¶

In [ ]:
fig = plt.figure(figsize=(12, 9))
gs = fig.add_gridspec(nrows=2, ncols=2)
#
ax = fig.add_subplot(gs[:,0])
x = np.random.randn(1000)
ax.hist(x, bins=100, rwidth=0.8)
#
ax = fig.add_subplot(gs[0,1])
x = np.linspace(0, 2*math.pi, 100)
y = scipy.sin(x)
ax.plot(x,y)
#
ax = fig.add_subplot(gs[1,1])
x = np.linspace(0, 2*math.pi, 100)
y = scipy.cos(x)
ax.plot(x,y)

# plt.savefig('grids.png', dpi=200)
plt.show()
/Users/localadmin/opt/anaconda3/envs/TIL6022/lib/python3.7/site-packages/ipykernel_launcher.py:10: DeprecationWarning:

scipy.sin is deprecated and will be removed in SciPy 2.0.0, use numpy.sin instead

/Users/localadmin/opt/anaconda3/envs/TIL6022/lib/python3.7/site-packages/ipykernel_launcher.py:15: DeprecationWarning:

scipy.cos is deprecated and will be removed in SciPy 2.0.0, use numpy.cos instead

Challenge I¶

Add title and label to the figure above.

In [ ]:
# YOUR CODE HERE
/Users/localadmin/opt/anaconda3/envs/TIL6022/lib/python3.7/site-packages/ipykernel_launcher.py:13: DeprecationWarning:

scipy.sin is deprecated and will be removed in SciPy 2.0.0, use numpy.sin instead

/Users/localadmin/opt/anaconda3/envs/TIL6022/lib/python3.7/site-packages/ipykernel_launcher.py:21: DeprecationWarning:

scipy.cos is deprecated and will be removed in SciPy 2.0.0, use numpy.cos instead

Seaborn¶

Scatterplot¶

iris dataset: multiple species with following information

  • Sepal & petal
  • Length and width
In [ ]:
iris = sns.load_dataset('iris')
iris.head()
Out[ ]:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
In [ ]:
sns.scatterplot(x='sepal_length', y='sepal_width', data=iris)
plt.show()

Challenge II¶

Color represents the hue

In [ ]:
# YOUR CODE HERE

Relplot¶

In [ ]:
sns.relplot(x='sepal_length', y='sepal_width', data=iris, hue='species')
plt.show()
In [ ]:
sns.relplot(x='petal_length', y='petal_width', data=iris, hue='species')
# plt.savefig('iris_petal.png', dpi=300)
plt.show()
In [ ]:
fmri = sns.load_dataset("fmri")
sns.relplot(data=fmri, x="timepoint", y="signal", kind="line")
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fbad2704f90>

Jointplot¶

tips dataset

In [ ]:
tips = sns.load_dataset('tips')
tips.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [ ]:
sns.jointplot(x='total_bill', y='tip', data=tips)
# plt.savefig('total_bill_tip.png', dpi=200)
plt.show()

Boxplot / Violinplot¶

In [ ]:
fig, ax = plt.subplots(ncols=2, figsize=(15,9))
sns.boxplot(x='species', y='petal_width', data=iris_data, ax=ax[0])
sns.violinplot(x='species', y='petal_width', data=iris_data, ax=ax[1])
# fig.savefig('iris_petal_width_dist.png', dpi=200)
plt.show()

FacetGrid¶

In [ ]:
g = sns.FacetGrid(tips, col='sex', hue='smoker')
g.map(sns.scatterplot, 'total_bill', 'tip')
g.add_legend()
plt.savefig('smoker.png', dpi=200)
plt.show()

Heatmap¶

In [ ]:
conf_mat = np.array([
    [85, 3, 2, 10, 0],
    [1, 80, 10, 1, 8],
    [0, 0, 70, 20, 10],
    [5, 2, 3, 75, 15],
    [10, 0, 0, 0, 90]
])
sns.heatmap(conf_mat, cmap='YlGn', annot=True)

# fix for mpl bug that cuts off top/bottom of seaborn viz
# https://github.com/mwaskom/seaborn/issues/1773
b, t = plt.ylim() # discover the values for bottom and top
b += 0.5 # Add 0.5 to the bottom
t -= 0.5 # Subtract 0.5 from the top
plt.ylim(b, t) # update the ylim(bottom, top) values

# plt.savefig('heatmap.png', dpi=200)
plt.show()

Plotly Express¶

In [ ]:
df = px.data.iris() 
fig = px.scatter(df, x="sepal_length", y="sepal_width")
fig.show()

Challenge III¶

Add species as the color and hover is also species

In [ ]:
# YOUR CODE HERE
In [ ]:
df = px.data.tips()
fig = px.histogram(
    df, x="total_bill", y="tip", color="sex", 
    hover_data=df.columns
)
fig.show()
In [ ]:
#load data
df_2007 = df.query('year==2007')
fig = px.bar(df_2007, x="pop", y="continent", orientation='h')
fig.show()

Challenge IV¶

In [ ]:
# YOUR CODE HERE

Plotly Graph Objects¶

In [ ]:
data_year = df.query("year==2007")
fig = go.Figure(data=go.Scatter(
    x = data_year['gdpPercap'],
    y = data_year['lifeExp'],
    mode='markers'
))
fig.update_xaxes(title_text="gdpPercap")
fig.update_yaxes(title_text="lifeExp")
#fig.update_xaxes(type="log")
fig.show()
In [ ]:
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp")
fig.show()
In [ ]:
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp", log_x=True)
fig.show()

Challenge V¶

Try to recreate the below figure using plotly express for population of 2007 from df.

  • color is the continent
  • size of the bubble is population
  • hover is country

Hint

  • Scatter functionality
  • Scatter example
In [ ]:
# YOUR CODE HERE
In [ ]: